import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: tao
 * Date: 2023-11-13
 * Time: 12:48
 */
public class JdbcTest {
    public static void main(String[] args) throws SQLException {
        //1、创建数据源
        DataSource dataSource = new MysqlDataSource();
        //指定操作的数据库
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java110?characterEncoding=utf8&&useSSL=false");
        //指定用户
        ((MysqlDataSource)dataSource).setUser("root");
        //密码
        ((MysqlDataSource)dataSource).setPassword("123456");

        //2、创建数据库服务器连接
        Connection connection = dataSource.getConnection();

        //3、创建sql语句
        String sql = "insert into student values(1, '张三')";
        //预处理
        PreparedStatement statement = connection.prepareStatement(sql);

        //4、把构造好的sql传给数据库服务器
        int n = statement.executeUpdate();
        System.out.println(n);

        //5、回收必要的资源
        statement.close();
        connection.close();
    }
}
